﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 场景传送门配置脚本
    /// </summary>
    [ExecuteInEditMode]
    public class SceneDoor : SceneObject
    {
#if UNITY_EDITOR
		private enum DoorTypeEnum
        {
			[EnumLabel("普通")]
			Normal=0,
			[EnumLabel("副本")]
			FB =1,
			[EnumLabel("团队副本")]
			TeamFB =10,
			[EnumLabel("不可见")]
			Invisible =100,
        }

		[Header("Door.xml表格ID")]
		[SerializeField]
		private int doorId;

		[SerializeField]
		[Tooltip("The type of door.")]
		[EnumLabel("Door Type")]
		private DoorTypeEnum doorType;

        [SerializeField]
        [Header("限制等级")]
        private int limitLevel;

        [SerializeField]
        [Header("传送目标场景ID")]
        private int targetSceneID;

        [SerializeField]
        [Header("传送目标场景中对应的门Id")]
        private int targetDoorID;

        [SerializeField]
        [Header("传送门位置偏移")]
        private Vector3 offset;

        [SerializeField]
        [Tooltip("传送门方向")]
        private Vector3 rotation;

        [SerializeField]
        [Header("角色位置偏移.")]
        private Vector3 roleOffset;

        [SerializeField]
        [Header("角色朝向旋转.")]
        private int roleRotation;

        [SerializeField]
        [Header("门资源 ID.")]
        private string resID = "4032";

        [Header("预览对象资源")]
		[SerializeField]
		private AssetID previewAssetId;

		// The preview prefab.
		private GameObject previewPrefab;

		public int ID { get { return this.doorId; } }
        public int DoorType { get { return (int)this.doorType; } }
        public int LimitLevel { get { return this.limitLevel; } }
        public int TargetSceneID { get { return this.targetSceneID; } }
        public int TargetDoorID { get { return this.targetDoorID; } }
        public Vector3 Offset { get { return this.offset; } }
        public Vector3 Rotation { get { return this.rotation; } }
        public Vector3 RoleOffset { get { return this.roleOffset; } }
        public int RoleRotation { get { return this.roleRotation; } }
        public string ResID { get { return this.resID; } }

        protected override bool HasPreview()
        {
            return this.previewPrefab != null;
        }

        protected override GameObject BuildPreview()
        {
            var obj = Instantiate(this.previewPrefab);
            obj.transform.localPosition = this.offset;
            obj.transform.localRotation = Quaternion.Euler(this.rotation);
            return obj;
        }

        void OnValidate()
        {
            if (Application.isPlaying) return;
            this.name = "Door" + this.doorId;
            if(this.doorType != DoorTypeEnum.Invisible)
            {
                if (!string.IsNullOrEmpty(previewAssetId.AssetGUID))
                {
                    this.previewPrefab = previewAssetId.LoadObject<GameObject>();
                }
            }
            else
            {
                //Debug.LogWarning("Can not find NPC with id: " + this.npcId);
                this.previewPrefab = null;
            }
            this.RefreshPreview();

            if(this.PreviewObject != null)
            {
                this.PreviewObject.transform.localPosition = this.offset;
                this.PreviewObject.transform.localRotation =  Quaternion.Euler(this.rotation);
            }

        }

#endif




    }
}

